import * as Cesium from 'cesium/Cesium'
import MeasureUnits from "./MeasureUnits";
import PolylineMeasurement from "./PolylineMeasurement";
import PointMeasurement from "./PointMeasurement";
import MeasurementMouseHandler from "./MeasurementMouseHandler";
import AreaMeasurement from "./AreaMeasurement";
import HeightMeasurement from "./HeightMeasurement";
import DistanceMeasurement from "./DistanceMeasurement";
import HorizontalMeasurement from "./HorizontalMeasurement";
import VerticalMeasurement from "./VerticalMeasurement";

function MeasureViewModel(e) {
    var i = (e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT)).scene
        , t = Cesium.defined(e.units) ? e.units : new MeasureUnits
        , r = Cesium.defined(e.primitives) ? e.primitives : i.primitives.add(new Cesium.PrimitiveCollection)
        , n = r.add(new Cesium.PointPrimitiveCollection)
        , a = r.add(new Cesium.LabelCollection)
        , o = new MeasurementMouseHandler(i)
        , s = {
        scene: i,
        units: t,
        locale: e.locale,
        points: n,
        labels: a,
        primitives: r
    }
        , l = Cesium.clone(s);
    l.showComponentLines = !0;
    var c = [new DistanceMeasurement(s), new DistanceMeasurement(l), new PolylineMeasurement(s), new HorizontalMeasurement(s), new VerticalMeasurement(s), new HeightMeasurement(s), new AreaMeasurement(s), new PointMeasurement(s)];
    this.expanded = !1;
    var u = Cesium.knockout.observable(!1);
    Cesium.knockout.defineProperty(this, "expanded", {
        get: function() {
            return u()
        },
        set: function(e) {
            e !== u() && (u(e),
                e ? this._activate() : this._deactivate())
        }
    }),
        this.instructionsVisible = !1,
        this.selectedMeasurement = void 0;
    var d = Cesium.knockout.observable();
    Cesium.knockout.defineProperty(this, "selectedMeasurement", {
        get: function() {
            return d()
        },
        set: function(e) {
            var t = d();
            Cesium.defined(t) && t.reset(),
                d(e),
                o.selectedMeasurement = e,
            i.requestRenderMode && i.requestRender()
        }
    }),
        Cesium.knockout.track(this, ["expanded", "instructionsVisible"]),
        this._measurements = c,
        this._units = t,
        this._mouseHandler = o,
        this._primitives = r,
        this._scene = i,
        this._removeListener = i.morphStart.addEventListener(MeasureViewModel.prototype.onMorph, this)
}

Object.defineProperties(MeasureViewModel.prototype, {
    scene: {
        get: function () {
            return this._scene
        }
    },
    measurements: {
        get: function () {
            return this._measurements
        }
    },
    units: {
        get: function () {
            return this._units
        }
    }
}),
    MeasureViewModel.prototype.toggleActive = function () {
        this.expanded = !this.expanded
    }
    ,
    MeasureViewModel.prototype.toggleInstructions = function () {
        this.instructionsVisible = !this.instructionsVisible
    }
    ,
    MeasureViewModel.prototype._activate = function () {
        this._mouseHandler.activate(),
            this.selectedMeasurement = this._measurements[0]
    }
    ,
    MeasureViewModel.prototype._deactivate = function () {
        this._mouseHandler.deactivate(),
            this.selectedMeasurement = void 0,
            this.reset()
    }
    ,
    MeasureViewModel.prototype.onMorph = function (e, t, i, r) {
        this.reset()
    }
    ,
    MeasureViewModel.prototype.reset = function () {
        this.instructionsVisible = !1,
            this._measurements.forEach(function (e) {
                e.reset()
            })
    }
    ,
    MeasureViewModel.prototype.isDestroyed = function () {
        return !1
    }
    ,
    MeasureViewModel.prototype.destroy = function () {
        return this._deactivate(),
            this._mouseHandler.destroy(),
            this._measurements.forEach(function (e) {
                e.destroy()
            }),
            this._scene.primitives.remove(this._primitives)
            , Cesium.destroyObject(this)
    }
;
export default MeasureViewModel;